define([
    'text!planmanage/draft/templates/Search.html',
], function(SearchTpl) {

    return fish.View.extend({
        template: fish.compile(SearchTpl),
        i18nData: fish.extend({}),
        events: {
            'click #search-btn': 'search'
        },

        serialize: function() {
            return this.i18nData;
        },

        afterRender: function() {
            var me = this;
            //初始化所属系统
            me.initNodeIdTree();
        },

        initNodeIdTree: function() {
            // 所属系统，下拉树
            var nodeId_setting = {
                async: {
                    enable: true,
                    contentType: "application/json",
                    url: webRoot + 'common/sysModule/queryAllForTree.do',
                    type: 'post'
                },
                view: {
                    dblClickExpand:false,
                    selectedMulti: false //如果为true,按住ctrl可以多选，false为单选 
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pId",
                        rootPId: 0,
                        iconSkin:"icon01"
                    }
                },
                callback: {
                    onClick: nodeIdonClick
                }
            };
            
            var nodeIdTree = $.fn.zTree.init($("#nodeId-tree-search"), nodeId_setting);
            var nodeIdTreeObj = $.fn.zTree.getZTreeObj("nodeId-tree-search");
            function nodeIdonClick(){
                nodes = nodeIdTree.getSelectedNodes();
                var nodeIdInput = $('input[name="nodeId"]');
                // 设置控件的其他属性：vId，为真正的value
                nodeIdInput.attr("vId",nodes[0].id).val(nodes[0].name);
                hideNodeIdTree();
            };
            //下拉树相对位置，随着窗口联动
            function nodeIdTreePos(){
                var inputObj = $('input[name="nodeId"]');
                var inputObjOffSet = $('input[name="nodeId"]').offset();
                    var $treePs = $("#nodeId-commbox-tree-search").css({left:inputObjOffSet.left + "px", top:inputObjOffSet.top + inputObj.outerHeight() + "px",width:inputObj.outerWidth() + "px",'z-index':9999});
                    return $treePs;
                    }
            $('input[name="nodeId"]').click(function(){
                nodeIdTreePos().slideDown("fast");
                $(window).resize(function(){
                    nodeIdTreePos();
                });
               $("body").bind("mousedown", onBodyDownNode);
            });
            function onBodyDownNode(event) {
                if (!(event.target.name == "nodeId" || event.target.id == "nodeId-commbox-tree-search" || $(event.target).parents("#nodeId-commbox-tree-datarecovery-search").length>0)) {
                    hideNodeIdTree();
                }
            };
            function hideNodeIdTree(){
                $("#nodeId-commbox-tree-search").fadeOut("fast");
                $("body").unbind("mousedown", onBodyDownNode);
            };

            // 执行方式
            $('input[name="execFrequency"]').combobox({
                placeholder : "请选择执行频率",
                dataTextField : "name",
                dataValueField : "value",
                setEditable : false,
                value : -1,
                dataSource : [ {
                    name : "全部",
                    value : -1
                }, {
                    name : "循环执行",
                    value : 0
                }, {
                    name : "单次执行",
                    value : 1
                }]
            });

            // 执行频率
            $('input[name="execType"]').combobox({
                placeholder : "请选择执行方式",
                dataTextField : "name",
                dataValueField : "value",
                setEditable : false,
                value : -1,
                dataSource : [ {
                    name : "全部",
                    value : -1
                }, {
                    name : "自动执行",
                    value : 1
                }, {
                    name : "手工执行",
                    value : 2
                }]
            });

            // 状态
            $('input[name="state"]').combobox({
                placeholder : "请选择状态",
                dataTextField : "name",
                dataValueField : "value",
                setEditable : false,
                value : "00A",
                dataSource : [ {
                    name : "全部",
                    value : 0
                }, {
                    name : "有效",
                    value : "00A"
                }, {
                    name : "无效",
                    value : "00X"
                }]
            });
        },

        search: function(){
            queryParam = $('.searchform').form('value'); //序列化表格内容为字符串
            queryParam.nodeId = $('#nodeId-tree').attr('vId');
            queryParam.state = '00D';
            if ($(".searchform").isValid()) {
                fish.trigger('SERACH_SHOW', queryParam);
            }
        }
    });
});
